/**
 * Created by yuxinhai on 2016/12/15.
 */

'use strict'

let mor = require("../libs/mongo/mongoHelper");
let querystring = require('querystring');
let dbr = require("../libs/mysql/mysqlHelper");
let jv = require("../libs/sys/myHelper").jv;
let http = require('request-promise');

app.use(app.routes.get('/wuye/ayi/list', async ctx => {
  ctx.render("wuye/ayi_list");
}));
app.use(app.routes.get('/wuye/ayi/add', async ctx => {
  ctx.render("wuye/ayi_card");
}));


app.use(app.routes.post('/wuye/ayi/list', async ctx => {
  let params = querystring.parse(ctx.req._parsedUrl.query);

  let userId = ctx.needLogin();
  if (!userId) {
    return;
  }


  let skip = ctx.req.json.skip;
  let take = ctx.req.json.take;

  let results = await dbr.find(`select w.id, w.username loginName, w.name,w.mobile,w.detailed_address address
from thwy_worker w
join thwy_user_worker u on (w.id = u.worker_id)
where u.user_id = :userId
order by u.create_date desc
limit :skip,:take`,
    {userId: userId, skip: skip, take: take});

  if (!results || !results.length) {
    ctx.body = [];
    return;
  }

  ctx.body = results;
}));

app.use(app.routes.post('/wuye/ayi/fuwu_list', async ctx => {
  let userId = ctx.needLogin();
  if (!userId) {
    return;
  }

  let results = await dbr.find(`select id,name from thwy_fuwu where fuwu_category_id = 10 and service_type='feeFuwu'`);

  ctx.body = results;
}));

app.use(app.routes.post('/wuye/ayi/add', async ctx => {
  let params = querystring.parse(ctx.req._parsedUrl.query);

  let userId = ctx.needLogin();
  if (!userId) {
    return;
  }

  let data = {};
  let results = {};
  let ayiId = ctx.req.id;

  if (ayiId) {
    results = await dbr.findOne(`select 1 from thwy_user_worker where user_id = :userId and worker_id = :workderId`, {
      userId: userId,
      workerId: ayiId
    });
    if (results && results.length) {
      ctx.msg = "已添加过该阿姨";
      return;
    }
  }

  if (!ayiId) {
    let workerId = jv.randomCode();
    data = jv.extend({
        id: workerId,
        "create_date": new Date(),
        update_date: new Date(),
        "password": "338a488a4106e8c99916628b11c27cea951c3cf632ef27554aa86547"
      },
      jv.retain(ctx.req.json, "name", "mobile", "address", "company_name"));

    data.detailed_address = data.address;
    delete data.address;

    data.username = data.mobile;

    results = await dbr.insert("thwy_worker", data);

    //测试返回对象。
    if (!results || !results.affectedRows) {
      ctx.msg = "添加失败";
      return;
    }

    results = await dbr.insert("thwy_worker_fuwu", ctx.req.json.fuwus.filter(it => {
      return it.value;
    }).map(it => {
      return {worker_id: workerId, fuwu_id: it.id}
    }));

    //测试返回对象。
    if (!results || !results.affectedRows) {
      ctx.msg = "添加失败";
      return;
    }

    ayiId = data.id;
  }

  results = await dbr.insert(`thwy_user_worker`, {
    worker_id: data.id,
    user_id: userId,
    create_date: new Date()
  });

  if (!results || !results.affectedRows) {
    ctx.msg = "添加失败";
    return;
  }

}));


app.use(app.routes.post('/wuye/ayi/remove', async ctx => {
  let params = querystring.parse(ctx.req._parsedUrl.query);

  let userId = ctx.needLogin();
  if (!userId) {
    return;
  }

  let data = {};
  let results = {};
  let ayiId = ctx.req.id;

  if (!ayiId) {
    ctx.msg = "找不到阿姨Id";
    return;
  }
  results = await dbr.exec(`delete from thwy_user_worker where user_id = :userId and worker_id = :workderId`, {
    userId: userId,
    workerId: ayiId
  });
  if (results && results.affectedRows) {
    ctx.msg = "解除失败";
    return;
  }
}));
